package cn.com.thinker.gen.maven.impl;

import cn.com.thinker.gen.maven.AbstractGenerateMojo;
import cn.com.thinker.tools.BeanUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;

/**
 * 代码生成器
 * 
 * @author crazyhc
 * @since 2017-05-16
 */
@Mojo(name = "code", threadSafe = true)
public class CodeGenerateMojo extends AbstractGenerateMojo {


	public void execute() throws MojoExecutionException, MojoFailureException {
		long startTime = System.currentTimeMillis();
		log.info("==========================准备生成文件...==========================");
		try{
			AutoGenerator gen = new AutoGenerator();
			gen.setGlobalConfig(this.getGlobalConfig());
			gen.setDataSource(this.getDataSource());
			gen.setStrategy(this.getStrategy());
			gen.setPackageInfo(this.getPackageInfo());
			TemplateConfig templateConfig = BeanUtils.copy(this.getTemplate(), TemplateConfig.class);
			gen.setTemplate(templateConfig);
			gen.execute();
		}catch (Exception e) {
			log.debug(e);
		}
		long endTime = System.currentTimeMillis();
		log.info("==========================耗时:"+((endTime-startTime)/1000)+"秒==========================");
		log.info("==========================文件生成完成！！！==========================");
	}

}
